<?php

use App\Http\Admin\Controller\CommonController;
use App\Util\Helper;
use Sc\Util\HtmlElement\El;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\ScTool;
use Sc\Util\Tool\Dir\EachFile;

/** @var $code */

Html::create("asd");
Html::css()->load("/webSite/grapes.min.css");
Html::js()->load("/webSite/grapes.min.js");
Html::css()->addCss(".gjs-devices-c .gjs-device-label{width: 35px}");
Html::loadAdminUtilJs();

$el = El::fromCode($code);

$scriptSrc = [];
foreach ($el->get('script') as $value) {
    if ($value->getAttr('src')) {
        $scriptSrc[] = $value->getAttr('src');
    }
}

$domain = Helper::route()->getCurrentUrl()->getDomain();
$assets = [];
ScTool::dir(BASE_PATH . '/public/resource')->each(function (EachFile $file) use (&$assets, $domain) {
    if (str_ends_with($file->filename, ".jpg")
        || str_ends_with($file->filename, ".png")
        || str_ends_with($file->filename, ".jpeg")
        || str_ends_with($file->filename, ".gif")
    ) {
        $assets[] = strtr($file->filepath, [BASE_PATH . '/public' => $domain]);
    }
});
$assets = json_encode($assets);
$scripts = json_encode($scriptSrc);
$page = Helper::request()->query('page');
$isSafe = Helper::request()->query('safe') ? 'false' : 'true';
$uploadUrl = sc_route([CommonController::class, 'upload']);
$data = base64_encode($code);

ob_start();
include(__DIR__ . '/edit-page.js.php');
Html::js()->defCodeBlock(ob_get_clean());

return "";